home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 16 windows forms / subclassingdemo / transparentform.vb < prev   
Encoding:
Text File  |  2002-03-16  |  4.4 KB  |  121 lines

  1. Public Class TransparentForm
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.     Friend WithEvents Label1 As System.Windows.Forms.Label
  26.     Friend WithEvents btnClose As System.Windows.Forms.Button
  27.  
  28.     'Required by the Windows Form Designer
  29.     Private components As System.ComponentModel.Container
  30.  
  31.     'NOTE: The following procedure is required by the Windows Form Designer
  32.     'It can be modified using the Windows Form Designer.  
  33.     'Do not modify it using the code editor.
  34.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  35.         Me.Label1 = New System.Windows.Forms.Label()
  36.         Me.btnClose = New System.Windows.Forms.Button()
  37.         Me.SuspendLayout()
  38.         '
  39.         'Label1
  40.         '
  41.         Me.Label1.BackColor = System.Drawing.Color.Red
  42.         Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
  43.         Me.Label1.Location = New System.Drawing.Point(160, 103)
  44.         Me.Label1.Name = "Label1"
  45.         Me.Label1.Size = New System.Drawing.Size(192, 40)
  46.         Me.Label1.TabIndex = 0
  47.         Me.Label1.Text = "Drag this form by clicking anywhere on its surface"
  48.         '
  49.         'btnClose
  50.         '
  51.         Me.btnClose.BackColor = System.Drawing.Color.BurlyWood
  52.         Me.btnClose.Location = New System.Drawing.Point(208, 175)
  53.         Me.btnClose.Name = "btnClose"
  54.         Me.btnClose.Size = New System.Drawing.Size(104, 40)
  55.         Me.btnClose.TabIndex = 1
  56.         Me.btnClose.Text = "Close "
  57.         '
  58.         'TransparentForm
  59.         '
  60.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  61.         Me.BackColor = System.Drawing.Color.Orange
  62.         Me.ClientSize = New System.Drawing.Size(512, 318)
  63.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnClose, Me.Label1})
  64.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  65.         Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
  66.         Me.Name = "TransparentForm"
  67.         Me.ResumeLayout(False)
  68.  
  69.     End Sub
  70.  
  71. #End Region
  72.  
  73.     ' in the Paint event we draw the elliptical form
  74.  
  75.     Private Sub TransparentForm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
  76.         ' make Black the transparent color, so that only the ellipse is visible
  77.         Me.TransparencyKey = Color.Blue
  78.  
  79.         ' Create a Brush of same color as backcolor
  80.         Dim b As New SolidBrush(Me.BackColor)
  81.  
  82.         ' draw a blue rectangle over the entire form.
  83.         e.Graphics.FillRectangle(Brushes.Blue, Me.ClientRectangle)
  84.         ' draw an ellipse of original backcolor
  85.         e.Graphics.FillEllipse(b, Me.ClientRectangle)
  86.         ' create a border
  87.         e.Graphics.DrawEllipse(Pens.Black, Me.ClientRectangle)
  88.  
  89.         ' destroy the Brush 
  90.         b.Dispose()
  91.     End Sub
  92.  
  93.     ' API constants for subclassing
  94.     Private Const WM_NCHITTEST As Integer = &H84
  95.     Private Const HTCAPTION As Integer = 2
  96.     Private Const HTCLIENT As Integer = 1
  97.  
  98.     ' we subclass the form so that the user can move it
  99.     ' by clicking anywhere on its surface
  100.  
  101.     Protected Overrides Sub WndProc(ByRef m As Message)
  102.         ' let the base form process this message
  103.         MyBase.WndProc(m)
  104.  
  105.         Select Case m.Msg
  106.             Case WM_NCHITTEST
  107.                 ' if on client area, make Windows believe it's on caption
  108.                 If m.Result.ToInt32 = HTCLIENT Then
  109.                     ' the only way to assing an IntPtr
  110.                     m.Result = New IntPtr(HTCAPTION)
  111.                 End If
  112.         End Select
  113.     End Sub
  114.  
  115.     ' just close the form
  116.  
  117.     Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
  118.         Me.Close()
  119.     End Sub
  120. End Class
  121.